<?php
namespace app\admin\controller;

/**
 * 上传
 * Class Upload
 * @package app\admin\controller
 */
class Upload extends Common
{

    /**
     * 图片上传
     * @return array|string
     */
    public function image()
    {
        try {
            return uploadImage('file');
        } catch (\Exception $e) {
            return errorJson($e->getMessage());
        }
    }

    /**
     * 资料文件上传
     * @return array|string
     */
    public function data()
    {
        try {
            $option = [
                'ext' => 'xls',
                'type' => 'application/zip,application/vnd.ms-excel,application/vnd.ms-',
                'size' => 1024 * 1024 * 2
            ];
            return uploadFile('file', 'download', $option, false);
        } catch (\Exception $e) {
            return errorJson($e->getMessage());
        }
    }

    /**
     * 视频文件上传
     * @return array|string
     */
    public function video()
    {
        try {
            $option = [
                'ext' => 'mp4',
                'type' => 'video/mp4',
                'size' => 1024 * 1024 * 50
            ];
            return uploadFile('file', 'video', $option, false);
        } catch (\Exception $e) {
            return errorJson($e->getMessage());
        }
    }

    public function serverFile()
    {
        $catalog = requestValue('catalog');
        $root = '/uploads/image/' . $catalog;
        $list = scandir('.' . $root);
        $data = [];
        foreach ($list as $item) {
            if ($item != '.' && $item != '..') {
                $path = $root . $item;
                $dir = '.' . $path;
                $data[] = [
                    'name' => $item,
                    'is_folder' => is_dir($dir),
                    'path' => $path,
                    'size' => sizeConversion(filesize($dir)),
                    'time' => date('Y-m-d H:i:s', filectime($dir)),
                    'time_int' => filectime($dir)
                ];
            }
        }
        exit(json_encode(arraySort($data, 'time_int', SORT_DESC)));
    }

    public function index()
    {
        return $this->fetch();
    }
}
